package cn.nawang.ebeim.client.transfer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.util.PropertiesConstants;
import cn.nawang.ebeim.common.entity.FileInfo;

public class Client {
    private static final Logger LOG = LoggerFactory.getLogger(Client.class);
  private String host;
  private Integer port;

  public Client(String host, int port) {
    this.host = host;
    this.port = port;
  }

  public void doTask(FileInfo info) throws Exception {
    if (info.getType() == FileInfo.TYPE_FILE_UPLOAD
        || info.getType() == FileInfo.TYPE_VERSION_UPLOAD) {
      HttpUploadQueryClient client = new HttpUploadQueryClient(host, port);
      client.run(info);
    } else {
      try{
          HttpDownloadClient client = new HttpDownloadClient(host, port);
          client.run(info);
      }catch(Exception e){
          if (info.getTryCount() < PropertiesConstants.TRY_COUNT){
              info.setTryCount(info.getTryCount()+1);
              LOG.info("第"+info.getTryCount()+"次重试----"+e.getMessage());
              doTask(info);
          }else{
              throw e;
          }
      }
    }
  }
}
